package com.admin.action;

import com.admin.bean.Tag;
import com.admin.service.TagAdminService;
import com.data.util.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/tag/")
public class TagAdminAction {
    @Autowired
    private TagAdminService tagAdminService;

    /***
     * 标签列表
     * @param request
     * @return
     */
    @GetMapping("list")
    public String listTagAction(HttpServletRequest request) {
        List<Tag> list = tagAdminService.listTagService();
        request.setAttribute("dataMap", list);
        return "WEB-INF/admin/tag/taglist";
    }

    /**
     * 进入到添加标签页
     *
     * @return
     */
    @GetMapping("input")
    public String inputAddTagAction() {
        return "WEB-INF/admin/tag/addtag";
    }

    /**
     * 添加标签
     *
     * @param tag
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public Map<String, Object> addTagAction(Tag tag) {
        boolean bool = tagAdminService.addTagService(tag);
        return JsonUtils.returnMassageUtils(bool, bool ? "添加成功" : "添加失败");
    }

    /**
     * 删除标签
     *
     * @param id
     * @return
     */
    @PostMapping("delete")
    @ResponseBody
    public Map<String, Object> deleteTagAction(String id) {
        boolean bool = tagAdminService.deleteTagService(id);
        return JsonUtils.returnMassageUtils(bool, bool ? "删除成功" : "删除失败");
    }

}
